/*
 * Copyright (c) 2003
 * XDoclet Team
 * All rights reserved.
 */
package org.xdoclet.plugin.qtags.impl.test;

/** @noinspection PointlessBooleanExpression*/
public class FooBarTagImpl extends org.xdoclet.XDocletTag implements org.xdoclet.plugin.qtags.impl.test.FooBarTag {
    public static final String NAME = "foo.bar";
    private static final java.util.List ALLOWED_PARAMETERS = java.util.Arrays.asList( new String[] {
        "age",
        "age-list",
        "apple",
        "apple-list",
        "funny",
        "funny-list",
        "grape-fruit",
        "grape-fruit-list",
        ""
    });
    
    private static final java.util.List ALLOWED_VALUES = java.util.Arrays.asList( new String[] {
        ""
    });
    public FooBarTagImpl(String name, String value, com.thoughtworks.qdox.model.AbstractBaseJavaEntity entity, int lineNumber, org.xdoclet.QDoxPropertyExpander expander) {
        super(name, value, entity, lineNumber, expander);
    }
    public FooBarTagImpl(String name, String value, com.thoughtworks.qdox.model.AbstractBaseJavaEntity entity, int lineNumber) {
        super(name, value, entity, lineNumber);
    }

    public int getAge() {
        boolean required = false;
        String result = getNamedParameter("age");
        if(required && result == null) {
            bomb("age=\"???\" must be specified.");
        }

        int retVal = 0;


        if (result != null) {

            
                try {
                    retVal = Integer.decode(result).intValue();
                } catch(NumberFormatException nfe) {
                    bomb("age=\"" + result + "\" is not valid integer");
                    throw nfe;
                }
        }
        
        return retVal;
    }
    public int[] getAgeList() {
        boolean required = false;
        String result = getNamedParameter("age-list");
        if(required && result == null) {
            bomb("age-list=\"???\" must be specified.");
        }

        int[] retVal = null;


        if (result != null) {
            java.util.StringTokenizer strTok = new java.util.StringTokenizer(result, ",");
            retVal = new int[strTok.countTokens()];
            int idx = 0;
            String token;

            
            while (strTok.hasMoreTokens()) {
                token = strTok.nextToken(); 
                try {
                    retVal[idx++] = Integer.decode(token).intValue();
                } catch(NumberFormatException nfe) {
                    bomb("age-list=\"" + token + "\" is not valid integer");
                    throw nfe;
                }
            }
        }
        
        return retVal;
    }
    public java.lang.String getApple() {
        boolean required = false;
        String result = getNamedParameter("apple");
        if(required && result == null) {
            bomb("apple=\"???\" must be specified.");
        }

        java.lang.String retVal = null;

        if(result == null) {
            result = "green";
        }

        if (result != null) {

            
                if( !(false || result.equals("red")|| result.equals("green")) ) {
                    // todo we should say what file and line number too
                    bomb("apple=\"" + result + "\" is an invalid parameter value.");
                }
                retVal = result;
        }
        
        return retVal;
    }
    public java.lang.String[] getAppleList() {
        boolean required = false;
        String result = getNamedParameter("apple-list");
        if(required && result == null) {
            bomb("apple-list=\"???\" must be specified.");
        }

        java.lang.String[] retVal = null;

        if(result == null) {
            result = "green";
        }

        if (result != null) {
            java.util.StringTokenizer strTok = new java.util.StringTokenizer(result, ",");
            retVal = new java.lang.String[strTok.countTokens()];
            int idx = 0;
            String token;

            
            while (strTok.hasMoreTokens()) {
                token = strTok.nextToken(); 
                if( !(false || token.equals("red")|| token.equals("green")) ) {
                    // todo we should say what file and line number too
                    bomb("apple-list=\"" + token + "\" is an invalid parameter value.");
                }
                retVal[idx++] = token;
            }
        }
        
        return retVal;
    }
    public boolean isFunny() {
        boolean required = false;
        String result = getNamedParameter("funny");
        if(required && result == null) {
            bomb("funny=\"???\" must be specified.");
        }

        boolean retVal = false;

        if(result == null) {
            result = "true";
        }

        if (result != null) {

            
                retVal = Boolean.valueOf(result).booleanValue();
        }
        
        return retVal;
    }
    public boolean[] isFunnyList() {
        boolean required = false;
        String result = getNamedParameter("funny-list");
        if(required && result == null) {
            bomb("funny-list=\"???\" must be specified.");
        }

        boolean[] retVal = null;

        if(result == null) {
            result = "true";
        }

        if (result != null) {
            java.util.StringTokenizer strTok = new java.util.StringTokenizer(result, ";");
            retVal = new boolean[strTok.countTokens()];
            int idx = 0;
            String token;

            
            while (strTok.hasMoreTokens()) {
                token = strTok.nextToken(); 
                retVal[idx++] = Boolean.valueOf(token).booleanValue();
            }
        }
        
        return retVal;
    }
    public java.lang.String getGrapeFruit() {
        boolean required = true;
        String result = getNamedParameter("grape-fruit");
        if(required && result == null) {
            bomb("grape-fruit=\"???\" must be specified.");
        }

        java.lang.String retVal = null;


        if (result != null) {

            
                retVal = result;
        }
        
        return retVal;
    }
    public java.lang.String[] getGrapeFruitList() {
        boolean required = true;
        String result = getNamedParameter("grape-fruit-list");
        if(required && result == null) {
            bomb("grape-fruit-list=\"???\" must be specified.");
        }

        java.lang.String[] retVal = null;


        if (result != null) {
            java.util.StringTokenizer strTok = new java.util.StringTokenizer(result, ",");
            retVal = new java.lang.String[strTok.countTokens()];
            int idx = 0;
            String token;

            
            while (strTok.hasMoreTokens()) {
                token = strTok.nextToken(); 
                retVal[idx++] = token;
            }
        }
        
        return retVal;
    }

    protected void validateLocation() {
        if(isOnClass) {
            bomb("is not allowed on classes");
        }
        if(isOnField) {
            bomb("is not allowed on fields");
        }
        
        // check uniqueness

        // warn deprecation
        System.err.println("@" + getName() + ":"  + getValue());
        
        // check for allowed values for whole tag
        if( ALLOWED_VALUES.size() > 1 && !ALLOWED_VALUES.contains(getValue())) {
            bomb( "\"" + getValue() +"\" is not a valid value. Allowed values are ");
        }        
        // Verify that all parameters are known.
        final java.util.Collection parameterNames = getNamedParameterMap().keySet();
        for (java.util.Iterator iterator = parameterNames.iterator(); iterator.hasNext();) {
            String parameterName = (String) iterator.next();
            if (!ALLOWED_PARAMETERS.contains(parameterName)) {
                bomb(parameterName + " is an invalid parameter name.");
            }
        }
        
        // Get all the parameters to validate their contents
        getAge();
        getAgeList();
        getApple();
        getAppleList();
        isFunny();
        isFunnyList();
        getGrapeFruit();
        getGrapeFruitList();
    }

    public void validateModel() {
        // check uniqueness
    }
}